﻿using OpenAuth.App.Interface;
using OpenAuth.App.Request;
using OpenAuth.App.Response;
using OpenAuth.Repository;
using OpenAuth.Repository.Domain;
using OpenAuth.Repository.Interface;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using System.Linq;
using Infrastructure;

namespace OpenAuth.App.JY
{
   public class PLA_PROJECT_ALLApp:BaseApp<PLA_PROJECT_ALL, OpenAuthDBContext>
    {
        private DbExtension _dbExtension;
        public async Task<TableData> Load(PLA_PROJECT_ALLListReq request) 
        {
            var loginContext = _auth.GetCurrentUser();
            if (loginContext == null) {
                throw new CommonException("登录已过期",Define.INVALID_TOKEN);
            }
           
        var properties = _dbExtension.GetProperties("PLA_PROJECT_ALL");
            if (properties == null || properties.Count == 0)
            {
                throw new Exception("当前登录用户没有访问该模块字段的权限，请联系管理员配置");
            }


            var propertyStr = string.Join(',', properties.Select(u => u.Key));
        

            var result = new TableData();
            var objs = UnitWork.Find<PLA_PROJECT_ALL>(null);
            if (!string.IsNullOrEmpty(request.key)) {
                objs.Where(u => u.PROJECT_NAME.Contains(request.key) ||u.PROJ_SHORT_NAME.Contains(request.key));
            }
            result.data = objs.OrderBy(u => u.FOUND_DATE).Skip((request.page - 1) * request.limit).Take(request.limit).Select($"new ({ propertyStr})");
            result.columnHeaders = properties;
            result.count = objs.Count();
            return result;
        }
        public void Add(PLA_PROJECT_ALL req) {

            //todo:补充或调整自己需要的字段
            req.FOUND_DATE = DateTime.Now;
            var user = _auth.GetCurrentUser().User;
            req.FOUND_MAN = user.Id;
             
            UnitWork.Add(req);
            UnitWork.Save();
        }
        public void Upp(PLA_PROJECT_ALL req)
        {
            UnitWork.Update<PLA_PROJECT_ALL>(u => u.Id == req.Id, u => (new PLA_PROJECT_ALL
            {
                PROJ_JC = req.PROJ_JC
            }));
           
 
        }
        public  PLA_PROJECT_ALLApp(IUnitWork<OpenAuthDBContext> unitWork, IRepository<PLA_PROJECT_ALL, OpenAuthDBContext> repository, IAuth auth, DbExtension dbExtension) : base(unitWork, repository, auth)
        {
            _dbExtension = dbExtension;
        }
       
    }
}
